<template>
	<view class="defaultStyles">

	</view>
</template>
<script lang="uts">
	import View from 'android.view.View'
	import { UTSAndroid } from "io.dcloud.uts";
	import MapView from 'com.amap.api.maps.MapView'
	import AMap from 'com.amap.api.maps.AMap'

	import AMapLocation from 'com.amap.api.location.AMapLocation'
	import JKLocationHelper from './JKLocationHelper.uts';

	import { jkInnerInitActivityLifecycle, ActivityLifeEvent, EventListener } from './jk-lifecycle.uts';
	import JKAMapHelper from './JKAMapHelper.uts';
	import { SuggestionItem } from './JKAMapHelper.uts';
	import BaseJkAmapView from 'com.open.jack.jk_amap.BaseJkAmapView'
	import MutableList from 'kotlin.collections.MutableList'
	import HashMap from 'java.util.HashMap'



	// function runInMain(codes : () => void) {
	// 	UTSAndroid.getDispatcher("main").async(function (_) {
	// 		codes()
	// 	}, null)
	// }

	// (event => {
	// 	console.log("1111 ----",this.mapView)
	// 	switch (event) {
	// 		case "ON_RESUME":
	// 			this.mapView?.onResume()
	// 			break
	// 		case "ON_PAUSE":
	// 			this.mapView?.onPause()
	// 			break
	// 		case "ON_DESTROY":
	// 			this.mapView?.onDestroy()
	// 			break
	// 	}
	// })


	class LifeEventListener extends EventListener {

		comp : UTSComponent<BaseJkAmapView>;
		listener : (event : ActivityLifeEvent) => void
		constructor(com : UTSComponent<BaseJkAmapView>, callback : (event : ActivityLifeEvent) => void) {
			super();
			this.comp = com
			this.listener = callback
		}
		override onChange(event : ActivityLifeEvent) {
			//			console.log("onChange ----", event)
			this.listener(event)
			//this.comp.onResume(//$emit("bindended")
		}
	}


	//原生提供以下属性或方法的实现  
	export default {
		name: "jk-amap",
		/**
		 * fetchGis地图选点,locate定位
		 */
		emits: ['fetchGis', "locate", "suggestions"],
		props: {
			// "mode": {
			// 	type: String,
			// 	default: ""
			// },
			// "chooseLatLng": {
			// 	type: Boolean,
			// 	default: false
			// },
			// "autoLocation": {
			// 	type: Boolean,
			// 	default: false
			// },
			"autoLifeEvent": {
				type: Boolean,
				default: false
			},
			"enableFetchGis": {//地图选址
				type: Boolean,
				default: false
			},
			"enableLocation": {//允许定位
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				mapView: null as MapView | null,
				isInitFetchGis: false,
				isInitLocation: false,
				aMap: null as AMap | null,
				innerEnableFetchGis: false,
				innerEnableLocation: false,
				locationHelper: null as JKLocationHelper | null,
				aMapHelper: null as JKAMapHelper | null,
				isInitLifecycle: false,
				eventListener: null as EventListener | null

			}
		},
		watch: {
			autoLifeEvent: {
				handler(newValue : boolean) {
					//					console.log("autoLifeEvent", autoLifeEvent)
					if (newValue) {
						this.initLifecycle()
					}
				},
				immediate: false
			},
			"enableLocation": {
				handler(newValue : boolean) {
					if (!this.isInitLocation && newValue && !this.innerEnableLocation) {
						this.isInitLocation = true
						this.innerEnableLocation = newValue
						this.initLocation()
					}
				},
				immediate: false
			},
			"enableFetchGis": {
				handler(newValue : boolean) {
					if (newValue && !this.innerEnableFetchGis) {
						this.innerEnableFetchGis = newValue
						if (!this.isInitFetchGis) {
							this.isInitFetchGis = true
							this.aMapHelper!.initGeoSearch()
							this.aMapHelper!.initFetchGis()
							setTimeout(() => {
								this.aMapHelper!.addScreenCenterMarker()
							}, 500)
						}
					}
				},
				immediate: false
			},
		},
		//addMarker添加marker,changeCamera更新中心显示，清除所有marker
		expose: ['onResume', 'onPause', 'onDestroy', 'addMarker', 'setMarkerIcon', 'changeCamera', 'clearMap', 'startLocation',
			"stopLocation", "setZoomControlsEnabled", "setScrollGesturesEnabled", "initLifecycle", "suggestions"],
		methods: {
			initLifecycle() {
				//				console.log("initLifecycl", this.isInitLifecycle)
				if (this.isInitLifecycle == false) {
					this.eventListener = new LifeEventListener(this, (event => {
						//console.log("aMAP lifeEvent", event)
						switch (event) {
							case "ON_RESUME":
								console.log("ON_RESUME ----")
								this.onResume()
								break
							case "ON_PAUSE":
								console.log("ON_PAUSE ----")
								this.onPause()
								break
							case "ON_DESTROY":
								console.log("ON_DESTROY ----")
								this.onDestroy()
								break
						}
					}))
					jkInnerInitActivityLifecycle(this.eventListener!)
					this.isInitLifecycle = true
				}

			},
			suggestions(keyword : string) {
				this.aMapHelper!.requestSuggeston(keyword)
			},
			setMarkerIcon(iconPath : string) {//setMarkerIcon
				this.aMapHelper!.setMarkerIcon(iconPath)
			},
			/**
				 * 在地图上添加marker
				 */
			addMarker(latitude : number, longitude : number) {
				this.aMapHelper!.addMarker(latitude, longitude)
			},
			//中心点
			changeCamera(latitude : number, longitude : number) {
				this.aMapHelper!.changeCamera(latitude, longitude)
			},
			clearMap() {
				this.aMapHelper!.clearMap()
			},
			//定位服务
			initLocation() {
				//				console.log("initLocation")
				if (this.locationHelper == null) {
					// 设置定位监听
					this.locationHelper = new JKLocationHelper((location : AMapLocation | null) => {
						console.log("location", location)
						if (location != null) {
							const longitude = location.getLongitude()
							const latitude = location.getLatitude()
							const province = location.getProvince()
							const city = location.getCity()
							const district = location.getDistrict()
							const addressName = location.getAddress()
							this.$emit('locate', {
								longitude, latitude, province, city, district, addressName
							})
							this.changeCamera(latitude, longitude)
						}

					})
				}
				this.locationHelper?.initLocation()
			},
			initInputtips() {
				//listener : (listString: MutableList<HashMap<String, String>>,errMsg :String) => void
				this.aMapHelper!.initInputtips((suggestions : SuggestionItem[] | null) => {
					this.$emit('suggestions', {
						suggestions
					})
				})
			},
			startLocation() {
				this.locationHelper?.start()
			},
			stopLocation() {
				this.locationHelper?.stop()
			},
			onResume() {
				if (this.mapView != null) {
					//					console.log("mapView onResume")
					this.mapView?.onResume()
				} else {
					//console.log("mapView ==null")
				}
			},
			onPause() {
				if (this.mapView != null) {
					//					console.log("mapView onPause")
					this.mapView?.onPause()
				} else {
					//console.log("mapView ==null")
				}
			},
			onDestroy() {
				this.locationHelper?.onDestroy()
				if (this.mapView != null) {
					//console.log("mapView onDestroy")
					this.mapView?.onDestroy()
				} else {
					//console.log("mapView ==null")
				}
			},
			//设置地图默认的缩放按钮是否显示
			setZoomControlsEnabled(enable : boolean) {
				if (this.$el != null) {
					this.$el!.mUiSettings?.setZoomControlsEnabled(enable)
				}
			},
			//设置地图是否可以手势滑动
			setScrollGesturesEnabled(enable : boolean) {
				if (this.$el != null) {
					this.$el!.mUiSettings?.setScrollGesturesEnabled(enable)
				}
			},

		},

		NVLoad() : BaseJkAmapView {
			let jAmapView = new BaseJkAmapView($androidContext!)
			let mapView = jAmapView.mapView
			this.mapView = mapView
			console.log("NVLoad mapView", this.mapView)
			return jAmapView
		},

		NVLoaded() {
			JKAMapHelper.initialize()
			const mapHelper = new JKAMapHelper(this)
			mapHelper.initMapView()
			this.setZoomControlsEnabled(false)
			this.aMapHelper = mapHelper
			if (this.$el != null) {
				//this.$el!.visibility = View.VISIBLE
				if (this.aMap == null) {
					this.aMap = this.$el!.mapView?.getMap();
					if (this.enableFetchGis) {
						mapHelper.initGeoSearch()
						mapHelper.initFetchGis()
						this.isInitFetchGis = true
					}
					if (this.enableLocation) {
						this.initLocation()
						this.isInitLocation = true
					}

					this.initInputtips()
				}
			}

		},
		NVBeforeUnload() {
			//console.log("NVBeforeUnload")
		},
		NVUnloaded() {
			//console.log("NVUnloaded")
		},

	}
</script>
<style>
	/* 定义默认样式值, 组件使用者没有配置时使用 */
	.defaultStyles {
		width: 750rpx;
		height: 240rpx;
	}
</style>